//
//  CarBossViewController.m
//  MansApp
//
//  Created by lanou3g on 15/7/16.
//  Copyright (c) 2015年 ACH. All rights reserved.
//

#import "CarBossViewController.h"
#import "SportViewController.h"
#import "CarCollectionViewCell.h"
#import "CarModel.h"
#import "CarPicViewController.h"

typedef void(^Block)();
@interface CarBossViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UIScrollViewDelegate>
@property(nonatomic,strong)SportViewController *carController;
@property(nonatomic,strong)SportViewController *useController;
@property(nonatomic,strong)UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *myDataArray;
@property(nonatomic,strong)UIView *myView;
@property(nonatomic,strong)UILabel *numberLabel;
@property(nonatomic,copy)Block myBlock;
@property(nonatomic,assign)NSInteger myRow;
@end

@implementation CarBossViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    
    

    self.carController = [[SportViewController alloc]init];
    self.carController.tag = 102;
    self.carController.view.frame =CGRectMake(0, 0, kWidth, kHeight - kP6Height(50)) ;
    [self addChildViewController: self.carController];
    [self.view addSubview: self.carController.view];
    
    __block CarBossViewController *Kself = self;
    self.myBlock= ^(){
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    Kself.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - kP6Height(50)) collectionViewLayout:layout];
    [Kself.view addSubview: Kself.collectionView];
    Kself.collectionView.delegate = Kself;
    Kself.collectionView.dataSource = Kself;
    Kself.collectionView.pagingEnabled = YES;
    Kself.collectionView.backgroundColor = [UIColor whiteColor];
    Kself.collectionView.showsHorizontalScrollIndicator = NO;
    Kself.myDataArray = [ACHCommon carModelAsynchronousURL:kCarPicURL collectionVIew:Kself.collectionView];
        

        
  
    };
    
 
    
    
    self.myView = [[UIView alloc]initWithFrame:CGRectMake(0, kHeight - kP6Height(50), kWidth, kP6Height(50))];
    self.myView.backgroundColor = [UIColor whiteColor];
    
    
    [self.view  bringSubviewToFront:self.carController.view];
    [self.view bringSubviewToFront:self.myView];
    [self  myControl];
    [self.view addSubview: self.myView];

  
}

//- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
//    
//    CarCollectionViewCell *cell = [self.collectionView visibleCells].firstObject;
//    NSIndexPath *path =[ self.collectionView indexPathForCell:cell];
//  
//    
//    self.numberLabel.text = [NSString stringWithFormat:@"%ld / %lu",path.row + 1,(unsigned long)self.myDataArray.count];
//    if ((path.row + 1) % 4 == 0 ) {
//        CarModel *model = [[self.myDataArray objectAtIndex:path.row] firstObject];
//     NSMutableArray  *array = [ACHCommon carModelAsynchronousURL:model.next_url collectionVIew:self.collectionView];
//        
//        if (array && array.count > 0) {
//            [self.myDataArray addObjectsFromArray:array];
//        }
//        
//    }
//    
//    
//}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    
    return self.myDataArray.count;
    
    
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIdentifier = @"cell1";
    
    [collectionView registerClass:[CarCollectionViewCell class] forCellWithReuseIdentifier:@"cell1"];
    CarCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
    
    [cell setModel:self.myDataArray[indexPath.row]];
    [cell addTarget:self action:@selector(imageViewAction:)];
    
    NSLog(@"%ld",indexPath.row);
    self.myRow = indexPath.row;
    
    return cell;

    
    
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"123123");
}
- (void)imageViewAction:(UIGestureRecognizer*)sender{
   
    CarCollectionViewCell *cell =   (CarCollectionViewCell *) sender.view.superview.superview;
    NSUInteger row =  [self.collectionView indexPathForCell:cell].row;
       NSMutableArray  *array = self.myDataArray[row];
    UIImageView *imageView = (UIImageView *)sender.view;
    
    if (array.count > imageView.tag - 100) {
        
    CarModel *model = array[imageView.tag - 100];
    
    CarPicViewController *pic = [[CarPicViewController alloc]init];
    pic.picArray = model.array;
    
    
    [self presentViewController:pic animated:NO completion:nil];
    }
    
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    
    
    return 0;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    
    return 0;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    return UIEdgeInsetsMake(10, 5, 0, 5);
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    return CGSizeMake(kWidth , kHeight -kP6Height(50));
}

- (void)myControl{
    NSArray *buttonTitle = @[@"",@"综合",@"用车",@"车图鉴赏"];
    for (int i = 0 ; i < 4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tag = 100 + i;
        if (i == 0) {
            button.frame = CGRectMake(0, 0, kP6width(50), kP6Height(50));
            [button setBackgroundImage:[UIImage imageNamed:@"common_icon_return@3x"] forState:UIControlStateNormal];
        }
        else button.frame = CGRectMake(kP6Height(60) + (i - 1) * 80, 0, kP6width(70), kP6Height(50));
        
        [button setTitle:buttonTitle[i] forState:UIControlStateNormal];
//        [button setBackgroundColor:[UIColor redColor]];
        
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.myView addSubview:button];
        
    }
    
    self.numberLabel = [[UILabel alloc]initWithFrame:CGRectMake(kP6width(300), 0, kP6width(70), kP6Height(50))];
    self.numberLabel.textAlignment = NSTextAlignmentCenter;
    [self.myView addSubview:self.numberLabel];
}


- (void)buttonAction:(UIButton *)sender{
    switch (sender.tag) {
        case  100:
            [self dismissViewControllerAnimated:NO completion:nil];
            break;
        case 101:
            
            [self.view bringSubviewToFront:self.carController.view];
             [self.view bringSubviewToFront:self.myView];
            break;
        case 102:{
            
        
            [self.view  bringSubviewToFront:self.useController.view];
             [self.view bringSubviewToFront:self.myView];
            
        }
            break;
        default:
            
            if (!self.collectionView) {
                self.myBlock();
//                self.myDataArray = [ACHCommon carModelAsynchronousURL:kCarPicURL collectionVIew:self.collectionView];
               
            }
            
            
            
            [self.view bringSubviewToFront:self.collectionView];
            
            
            break;
    }
    
    
    
    
}


- (SportViewController *)useController{
    
    if (_useController == nil) {
        
        _useController = [[SportViewController alloc]init];
    
        [self addChildViewController:_useController];
       _useController.view.frame = CGRectMake(0, 0, kWidth,kHeight - kP6Height(50));
        [self.view addSubview:_useController.view];
        

        
    }
    
    return _useController;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
